package com.jjh.controller;

import com.jjh.consts.Result;
import com.jjh.pojo.User;
import com.jjh.service.UserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
@CrossOrigin
@Slf4j
public class UserController {

    @Resource
    private UserService userService;


    /**
     * 条件分页查询
     * @param page
     * @param size
     * @param map
     * @return
     */
   @PostMapping("/page/{page}/{size}")
    public Result page(@PathVariable("page")Integer page,
                       @PathVariable("size")Integer size,
                       @RequestBody Map<String,Object> map){

       return userService.page(page,size,map);
   }

   @Transactional
   @PostMapping("/add")
    public Result addUser(@RequestBody User user){

       return userService.addUser(user);

   }


    @PutMapping("/status/{id}/{status}")
    public Result changeStatus(@PathVariable("id")Integer id,@PathVariable("status")Integer status){
        return userService.changeStatus(id,status);
    }

    @PutMapping("/update")
    public Result updateUser(@RequestBody User user){
       return userService.updateUser(user);
    }

    @PostMapping("/delete")
    public Result deleteUsers(@RequestBody Map<String,List<Integer>> map){
        List<Integer> ids = map.get("ids");
        return userService.deleteByIds(ids);
    }


}
